package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exection.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired//redis相关依赖
    private StringRedisTemplate stringRedisTemplate;
    @Autowired//用户mapper
    private SystemUserMapper systemUserMapper;
    /**
     * 获取图形验证码的key和value，base64
     */
    @Override
    public CaptchaVo getCaptcha() {
//        设置图片长宽，里面数值量
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);
//        获取验证码的值，转化成小写
        String lowerCase = specCaptcha.text().toLowerCase();
//        根据公司进行平均自定义唯一id：uuid
        String key = "admin:login:" + UUID.randomUUID();
//        设置验证码的key value，有效时间，单位秒
        stringRedisTemplate.opsForValue().set(key,lowerCase,60, TimeUnit.SECONDS);
        return new CaptchaVo(specCaptcha.toBase64(),key);
    }

    /**
     * 登陆
     *
     * @param loginVo
     */
    @Override
    public String login(LoginVo loginVo) {
        if (loginVo.getCaptchaCode() == null){//判断验证码有没有
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
//       获取验证吗
        String code = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaCode());
        if (code == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
//        把验证码变为小写对比
        if (!code.equals(loginVo.getCaptchaCode().toLowerCase())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername,loginVo.getUsername());
        SystemUser systemUser = systemUserMapper.selectOne(systemUserLambdaQueryWrapper);
        if (systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        if (systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }
        //5.校验用户密码
        if (!systemUser.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        return "";
    }
}
